package dao;

import entity.Sort;
import entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class SortDao {
    //增
    public int add(Sort sort, Connection connection) throws SQLException {
        String sql = "insert into sort(sort_name) values (?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, sort.getSortName());

        int count = statement.executeUpdate();
        statement.close();
        return count;
    }




    //删
    public int delete(int sortId, Connection connection) throws SQLException {
        String sql = "delete from sort where sort_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,sortId);

        int count = statement.executeUpdate();


        statement.close();
        return count;

    }





    //改
        public int update(Sort sort, Connection connection) throws SQLException {
            String sql = "update sort set sort_name = ? where sort_id = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, sort.getSortName());
            statement.setInt(2, sort.getSortId());
            int count = statement.executeUpdate();
            statement.close();
            return count;
        }
    //查
    public Sort selectById(int sortId, Connection connection) throws SQLException {
        String sql = "select * from sort where sort_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, sortId);
        ResultSet resultSet = statement.executeQuery();
        Sort sort = null;
        while (resultSet.next()){
            String sortName = resultSet.getString("sort_name");
            sort = new Sort(sortId,sortName);
            break;
        }



        resultSet.close();
        statement.close();
        return sort;
    }
}
